package com.example.demo.controller;

import com.example.demo.common.JwtUtils;
import com.example.demo.common.ResultModel;
import com.example.demo.common.ResultModelTool;
import com.example.demo.entity.Propagandainfo;
import com.example.demo.entity.User;
import com.example.demo.service.PropagandainfoService;
import com.example.demo.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Positive;
import java.util.List;
@CrossOrigin
@Controller
@RequestMapping("/propagandainfos")
@Transactional
public class PropagandainfoController {
    @Resource
    PropagandainfoService propagandainfoService;


    @Resource
    private UserService userService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 简介信息新增
     * @param
     * @return
     */

    @PostMapping("")
    @ResponseBody
    public ResultModel insertproinfo(@RequestBody Propagandainfo propagandainfo){

        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1=userService.getUser(userId);
        if(user1==null){
            return new ResultModel(200,"token过期或者权限不足");
        }
        ResultModel resultmodel=new ResultModel();
        resultmodel.setCode(200);
        propagandainfoService.insertproinfo(propagandainfo);
        resultmodel.setData("添加成功");
        return ResultModelTool.handleResultModel(resultmodel);
    }
    /**
     *  简介信息列表
     * @param
     * @return
     */

    @GetMapping("")
    @ResponseBody
    public ResultModel selectAll(){

        List<Propagandainfo> list=propagandainfoService.selectAllproinfo();
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(list);
        return ResultModelTool.handleResultModel(resultModel);
    }
    /**
     *  简介信息单个查询
     * @param
     * @return
     */
    @GetMapping("/{id}")
    @ResponseBody
    public ResultModel selectid(@PathVariable(value = "id") @Positive(message = "{id}") int id){

        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(propagandainfoService.selectproinfo(id));
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     *  简介信息修改
     * @param
     * @return
     */
    @PutMapping ("")
    @ResponseBody
    public ResultModel updateproinfo( @RequestBody Propagandainfo propagandainfo){
        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1=userService.getUser(userId);
        if(user1==null){
            return new ResultModel(200,"token过期或者权限不足");
        }
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        propagandainfoService.updateproinfo(propagandainfo);
        resultModel.setData("修改成功");
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     *  简介信息删除
     * @param
     * @return
     */

    @DeleteMapping("/{id}")
    @ResponseBody
    public ResultModel deleteid(@PathVariable(value = "id") @Positive(message = "{id}") int id){
        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1=userService.getUser(userId);
        if(user1==null){
            return new ResultModel(200,"token过期或者权限不足");
        }
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        propagandainfoService.deleteproinfo(id);
        resultModel.setData("删除成功");
        return ResultModelTool.handleResultModel(resultModel);

    }

}
